$(function () {
    var tbody=window.document.getElementById("tbody-result");
    $.ajax({
        type:'GET',
        dataType:'json',
        url:contextPath+"all/video",
        success:function (data) {
            // console.log(data.count);
            // console.log(data);
            var str="";
            // var l=data.message.length();
            // console.log(l);
            for (var i=0;i<data.count;i++) {
                // console.log(data.list[i].course_name);
                str += "<tr>" +
                    "<td>" + i + "</td>" +
                    "<td>" + data.list[i].course_name+"</td>"+
                    "<td>" + data.list[i].course_dir+ "</td>" +
                    "<td>" + data.list[i].url + "</td>" +
                    "<td>" + data.list[i].nandu + "</td>" +
                    "<td><button onclick='update(this)'>修改</button><button onclick='del(this)'>删除</button><button onclick='save(this)'>保存</button></td>" +
                    "<td><button onclick='add(this)'>分配到学习计划</button></td>"+
                    "</tr>";
            }
            tbody.innerHTML=str;
        }
        ,
        error:function () {
            alert("查询失败")
        }
    })
});
$('#btn').on('click',function () {
    var table=document.getElementById("tbody-result");
    var ss=new Object();
    ss.course_dir=document.getElementById("menuname").value;
    $.ajax({
        type:'GET',
        url:contextPath+"all/video/"+ss.course_dir,
        success:function (data) {
            var str="";
            // var l=data.message.length();
            // console.log(l);
            console.log(data);
            for (var i=0;i<data.count;i++) {
                // console.log(data.list[i].course_name);
                str += "<tr>" +
                    "<td>" + i + "</td>" +
                    "<td>" + data.list[i].course_name+"</td>"+
                    "<td>" + data.list[i].course_dir+ "</td>" +
                    "<td>" + data.list[i].url + "</td>" +
                    "<td>" + data.list[i].nandu + "</td>" +
                    "<td><button onclick='update(this)'>修改</button><button onclick='del(this)'>删除</button><button onclick='save(this)'>保存</button></td>" +
                    "<td><button onclick='add(this)'>分配到学习计划</button></td>"+
                    "</tr>";
            }
            table.innerHTML=str;
        },
        error:function () {
            alert("查询失败");
        }
    })
});
function add(r) {
    var table=window.document.getElementById("tbody-result");
    var rorNums=table.rows.length;
    var i=r.parentNode.parentNode.rowIndex-1;
    // console.log(theRequest.name);
    var ss=new Object();
    ss.course_name=table.rows[i].cells[1].innerHTML;
    $.ajax({
        type:'PUT',
        url:contextPath+"tolearner/newvideo/"+theRequest.name,
        async: false,
        contentType: "application/json; charset=utf-8",
        data:JSON.stringify(ss),
        dataType:"json",
        success:function (data) {
            console.log(data);
        },
        error:function () {
            alert("查看失败");
        }
    })
}
function update(r) {    //修改页面信息
    var table=window.document.getElementById("tbody-result");
    var rorNums=table.rows.length;
    var i=r.parentNode.parentNode.rowIndex-1;
    // var ss=new Object();
    table.rows[i].cells[1].innerHTML="<input type='text' id='name' value=" + table.rows[i].cells[1].innerHTML+ " />";
    table.rows[i].cells[2].innerHTML="<input type='text' id='dir' value=" + table.rows[i].cells[2].innerHTML+ " />";
    table.rows[i].cells[3].innerHTML="<input type='text' id='url' value=" + table.rows[i].cells[3].innerHTML+ " />";
    table.rows[i].cells[4].innerHTML="<input type='text' id='nandu' value=" + table.rows[i].cells[4].innerHTML+ " />";
}
function save(r) {
    var table=window.document.getElementById("tbody-result");
    var i=r.parentNode.parentNode.rowIndex-1;
    var ss =new Object();
    ss.course_name=document.getElementById("name").value;
    ss.course_dir=document.getElementById("dir").value;
    ss.video_url=document.getElementById("url").value;
    ss.nandu=document.getElementById("nandu").value;
    $.ajax({
        type:'POST',
        url:contextPath+"update/video",
        async: false,
        contentType: "application/json; charset=utf-8",
        data:JSON.stringify(ss),
        dataType:"json",
        success:function (data) {
            console.log(data);
            // table.deleteRow(i);
            alert(data.message)
            window.location.reload();
        },
        error:function () {
            alert("修改失败");
        }
    })
}
function  del(r) {
    var i=r.parentNode.parentNode.rowIndex-1;
    document.getElementById("tbody-result").deleteRow(i);
}
